﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SharingCenter.Web.Mod.Learning
{
    using SharingCenter.Web.Classes;
    using SharingCenter.Web.Classes.Requests;
    using SharingCenter.Web.Classes.enums;
    using SharingCenter.Web.Classes.Learning;
    using SharingCenter.Web.Classes.Messages;
    using System.Xml;
    using System.IO;
    using System.Xml.Linq;
    using System.Xml.Serialization;
    public partial class Match : LearningPageBase
    {
        List<Sc_LearningProfile> _matchedProfiles = new List<Sc_LearningProfile>();
        Sc_LearningProfile profileToMatch = null;
        protected void Page_Init(object sender, EventArgs e)
        {
            if (Request.QueryString["id"] == null)
            {
                RedirectToDefaultPage();
            }

            Guid lpId = Guid.Empty;
            try
            {
                lpId = new Guid(Request.QueryString["id"]);
            }
            catch { RedirectToDefaultPage(); }

            profileToMatch = LearningOperations.GetProfileByProfileId(lpId);

            if (profileToMatch.UserId != Profile.UserId)
            {
                RedirectToDefaultPage();
            }

            LinqDataSource1.Selecting += new EventHandler<LinqDataSourceSelectEventArgs>(LinqDataSource1_Selecting);
            LinqDataSource1.Selected += new EventHandler<LinqDataSourceStatusEventArgs>(LinqDataSource1_Selected);
            lvMatchResult.ItemCommand += new EventHandler<ListViewCommandEventArgs>(lvMatchResult_ItemCommand);
            lvMatchResult.ItemDataBound += new EventHandler<ListViewItemEventArgs>(lvMatchResult_ItemDataBound);
            lnkSendRequest.Click += new EventHandler(lnkSendRequest_Click);
            
            if (profileToMatch != null)
            {
                MultiView1.ActiveViewIndex = 1;
                LearningProfileItemToMatch.LearningProfileDataItem = profileToMatch;
            }
            else
            {
                MultiView1.ActiveViewIndex = 0;
            }
        }

        void lnkSendRequest_Click(object sender, EventArgs e)
        {
            string comment = txtComment.Text.Trim();
            Guid rqsteeUserId = new Guid(hdnUserId.Value);
            Guid lpId = new Guid(hdnLearningProfileId.Value);
            LearningRequestContext cxt = new LearningRequestContext();
            cxt.Comment = comment;
            cxt.RequesteeProfileId = lpId;
            cxt.RequesteeUserId = rqsteeUserId;
            cxt.RequestorProfileId = new Guid(Request.QueryString["id"]);
            cxt.RequestorUserId = Profile.UserId;
            NickNameLink2.UserId = rqsteeUserId;

            Sc_SharedRequest request = RequestOperations.AddRequest(EntityType.Learning, cxt.RequesteeProfileId, cxt.RequestorUserId, cxt.RequesteeUserId, cxt.Serialize());
            mvRequest.ActiveViewIndex = 1;
            //((Panel)e.Item.FindControl("pnRequestTime")).Visible = true;
            //((Label)e.Item.FindControl("lblRequestTime")).Text = Utility.FriendlyDateTime(DateTime.Now, TimeDisplayEnum.LiteralHours, false);
            //MessageOperations.SendMessage(EntityType.Default, WebConsts.FAKE_GUID, new Guid(t[1]), "", EntityType.Request, request.RequestId, null);

        }

        void LinqDataSource1_Selected(object sender, LinqDataSourceStatusEventArgs e)
        {
            lblMatchCount.Text = e.TotalRowCount.ToString();
        }

        void LinqDataSource1_Selecting(object sender, LinqDataSourceSelectEventArgs e)
        {
            e.Result = LearningOperations.GetMatchedLearningProfiles(profileToMatch);
        }

        void lvMatchResult_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            
        }

        void lvMatchResult_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "RequestLearning":
                    RequestLearning(ref e);
                    break;
                default: break;
            }
        }

        private void RequestLearning(ref ListViewCommandEventArgs e)
        {
            //Sc_LearningProfile ptr = (Sc_LearningProfile)((ListViewDataItem)e.Item).DataItem;
            string uid = ((HiddenField)e.Item.FindControl("hdnUserId")).Value;
            string lpid = ((HiddenField)e.Item.FindControl("hdnLearningProfileId")).Value;
            hdnLearningProfileId.Value = lpid;
            hdnUserId.Value = uid;
            NickNameLink1.UserId = new Guid(uid);
            pnRequestWindow.Visible = true;
        }

        protected void btnRequestOpener_Load(object sender, EventArgs e)
        {
            ScriptManager.GetCurrent(Page).RegisterAsyncPostBackControl((Control)sender);
        }
    }
}
